package com.zenithmind.gateway.route;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder.Builder;
import org.springframework.cloud.gateway.route.builder.GatewayFilterSpec;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

/**
 * 教育学习相关服务路由配置器
 * 专门负责教室、考试、编程、图书馆服务的路由配置
 * 遵循单一职责原则：只负责教育学习相关的路由配置
 */
@Slf4j
@Component
public class EducationRouteConfigurer implements RouteConfigurer {

    @Override
    public void configureRoutes(Builder routes) {
        log.info("配置教育学习相关服务路由...");
        
        // 教室服务路由 - 课程、章节、课时
        routes.route("classroom-service-route", r -> r
            .path("/api/zenithMind/course/**",
                 "/api/zenithMind/courses/**",
                 "/api/zenithMind/chapter/**",
                 "/api/zenithMind/lesson/**",
                 "/api/zenithMind/v1/courses/**",
                 "/api/zenithMind/api/v1/courses/**", 
                 "/api/zenithMind/api/v1/chapters/**", 
                 "/api/zenithMind/api/v1/lessons/**")
            // 仅为API v1路径应用stripPrefix过滤器
            .filters(f -> pathFilterForApiV1(f, "/api/zenithMind/api/v1/"))
            .uri("lb://classroom-service"));
            
        // 考试服务路由 - 考试、题目、记录
        routes.route("exam-service-route", r -> r
            .path("/api/zenithMind/exam/**",
                 "/api/zenithMind/question/**",
                 "/api/zenithMind/record/**",
                 "/api/zenithMind/api/v1/exams/**", 
                 "/api/zenithMind/api/v1/exam-records/**",
                 "/api/zenithMind/api/v1/questions/**")
            // 仅为API v1路径应用stripPrefix过滤器
            .filters(f -> pathFilterForApiV1(f, "/api/zenithMind/api/v1/"))
            .uri("lb://exam-service"));
            
        // 编程服务路由 - 编程题目、代码提交、运行
        routes.route("coding-service-route", r -> r
            .path("/api/zenithMind/problem/**",
                 "/api/zenithMind/submission/**",
                 "/api/zenithMind/code/**",
                 "/api/zenithMind/api/v1/problems/**", 
                 "/api/zenithMind/api/v1/submissions/**", 
                 "/api/zenithMind/api/v1/code/**")
            // 仅为API v1路径应用stripPrefix过滤器
            .filters(f -> pathFilterForApiV1(f, "/api/zenithMind/api/v1/"))
            .uri("lb://coding-service"));
            
        // 图书馆服务路由 - 图书、借阅、阅读
        routes.route("library-service-route", r -> r
            .path("/api/zenithMind/library/**",
                 "/api/zenithMind/book/**",
                 "/api/zenithMind/borrow/**",
                 "/api/zenithMind/api/v1/library/**",
                 "/api/zenithMind/api/v1/books/**")
            // 仅为API v1路径应用stripPrefix过滤器
            .filters(f -> pathFilterForApiV1(f, "/api/zenithMind/api/v1/"))
            .uri("lb://library-service"));
    }

    @Override
    public String getConfigureName() {
        return "EducationRouteConfigurer";
    }

    @Override
    public int getOrder() {
        return 3;
    }
    
    /**
     * 路径过滤器工具方法 - 专用于/api/zenithMind/api/v1/前缀的路径
     * 如果路径以指定前缀开头，则移除前缀
     */
    private GatewayFilterSpec pathFilterForApiV1(GatewayFilterSpec f, String prefixPattern) {
        return f.rewritePath("/api/zenithMind/api/v1/(?<segment>.*)", "/api/zenithMind/${segment}");
    }
}
